package com.hddz.acquisition.activity.stock;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.hddz.acquisition.AcquistionApp;
import com.hddz.acquisition.AppSetting;
import com.hddz.acquisition.DialogJst;
import com.hddz.acquisition.GlobalConst;
import com.hddz.acquisition.R;
import com.hddz.acquisition.activity.BaseActivity;
import com.hddz.acquisition.fragment.stock.FragmentGroundingFree1;
import com.hddz.acquisition.fragment.stock.FragmentGroundingScan;
import com.hddz.acquisition.model.Command;
import com.hddz.acquisition.model.JsonRespose;

@ContentView(R.layout.activity_stock_grounding)
public class ScanActivity extends BaseActivity {
    private final String TAG = "GroundingActivity";
    @ViewInject(R.id.tab_vpager)
    private ViewPager tab_vpager;
    @ViewInject(R.id.lbl_main_title)
    private TextView lbl_main_title;
    @SuppressLint("UseSparseArrays")
    private Map<Integer, Fragment> mCacheFragments = new HashMap<Integer, Fragment>();
    private Fragment mFm = null;
    FragmentGroundingScan fragmentGroundingScan = null;
    FragmentGroundingFree1 fragmentGroundingFree1 = null;
    private Bundle bundle = null;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lbl_main_title.setText(R.string.menu_plsm);
        tab_vpager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager()));
        tab_vpager.setOffscreenPageLimit(2);

        baseSendCommand("1501", "");
    }


    @Override
    public void addResponse(JsonRespose json) {
        if (currentCmd.equals("1502")) {
            fragmentGroundingScan.updateResponse(json);
            fragmentGroundingFree1.updateList(json.response.ResponseList);
        } else if (currentCmd.equals("1503")) {
            Toast.makeText(ScanActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            finish();
        } else if (currentCmd.equals("1504")) {
            finish();
        }
        fragmentGroundingScan.appandMsg(json.message);
    }

    @Override
    public void addErr(String err) {
        if (currentCmd.equals("")) {
            Toast.makeText(ScanActivity.this, err, Toast.LENGTH_SHORT).show();
            finish();
        } else {
            fragmentGroundingScan.appandMsg(err);
        }
    }

    @Override
    public void receiveCode(String code) {
        fragmentGroundingScan.updateBarcode(code);
        if (currentCmd.equals("1501") || currentCmd.equals("1502")) {
            baseSendCommand("1502", code);
        }
    }

    @Override
    public void backDo() {
        onBack(null);
    }

    @Override
    public void finishDo() {
        onFinish(null);
    }

    private class MainViewPagerAdapter extends FragmentPagerAdapter {

        public MainViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Fragment getItem(int position) {
            if (mCacheFragments.containsKey(position)) {
                mFm = mCacheFragments.get(position);
            } else {
                switch (position) {
                    case 0:
                        fragmentGroundingScan = FragmentGroundingScan.newInstance();
                        mFm = fragmentGroundingScan;
                        break;
                    case 1:
                        fragmentGroundingFree1 = FragmentGroundingFree1.newInstance();
                        mFm = fragmentGroundingFree1;
                        break;
                    default:
                        break;
                }
                mCacheFragments.put(position, mFm);
            }
            return mFm;
        }
    }


    @Event(R.id.btn_back)
    private void onBack(View view) {
        dialogHelper.showAlertDialog("是否取消操作?", 24, new OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                baseSendCommand("1504", "");
            }
        });
    }

    @Event(R.id.btn_finish)
    private void onFinish(View view) {
        baseSendCommand("1503", "");
    }

}
